/**
 * 
 */
package com.zoiz.plus.extend;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author administrator
 * 
 */
public class CommonUtils {
	  static final Log log = LogFactory.getLog(CommonUtils.class);
	/**
	 * 检测web实际文件地址
	 * @param config
	 * @return
	 */
	public static String getWebAppPath(ServletContext config) {
		String webAppPath = config.getRealPath("/");
        if (webAppPath==null) {
            String err = "Cannot get web application path. Are you deploy the application as a .war file?";
            log.warn(err);
            throw new ExceptionInInitializerError(err);
        }
        if (!webAppPath.endsWith("/") && !webAppPath.endsWith("\\"))
            webAppPath = webAppPath + File.separator;
        log.info("Detect web application path: " + webAppPath);
        return webAppPath;
	}
	/**
	 * 加载配置文件
	 * @param file
	 * @return
	 */
	public static Properties readProperties(String file) {
		Properties props = new Properties();
		InputStream input = null;
		try {
			input = new BufferedInputStream(new FileInputStream(file));
			props.load(input);
		} catch (IOException ioe) {
			log.warn("Read properties file failed.", ioe);
		} finally {
			if (input != null) {
				try {
					input.close();
				} catch (IOException e) {
				}
			}
		}
		return props;
	}
}
